<?php
uses('controller');

class wishlistController extends Controller {

    function __construct() {
        parent::__construct();
    }

    /*
    4.没有找到相应的数据
    8.数据不完整
    */
    function wishlist() {
        $data['iid']    = trim($this->request['iid']);
        $data['act']    = trim($this->request['act']);

        //js
        $data['pagesize'] = intval($this->request['num']);
        $data['sort']     = trim($this->request['sort'])=='price' ? 'price1' : trim($this->request['sort'])=='rand' ? 'rand()' : 'a.updated';
        $data['sortorder']= trim($this->request['od'])=='asc' ? 'asc' : 'desc';

        if ($data['act'] == 'add' && intval($data['iid'])){
            $result = $this->newWishList($data); //新
        } elseif ($data['act'] == 'js' && $data['iid']) {
            $result = $this->wishlistList($data); //JS api
        } else {
            //user wishlists
            if ($this->request['s']) {
                $data['search'] = trim(str_replace(array('%','_','.'), '', $this->request['s']));
                if ($data['search']) $result = $this->wishlistList($data);
            }
            //$result['code']['wishlist'] = 4;
        }

        return $result;
    }

    /*
    1.添加成功
    2.数据库操作错误
    4.更新成功
    8.移除成功
    64.非法请求
    */
    function newWishList($data=null) {
        $this->debugmsg("wishlistController::newWishList");

        $this->needLogin();

        $data['iid']    = intval($this->request['iid']);
        $data['remove'] = $this->request['remove'];
        $data['uid']    = $this->session['id'];

        $result = $this->model->newWishList($data);
        $result['code']['wishlist'] = $result['code']['newWishList'];

        $result['data'] = $data;

        return $result;
    }

    function ajaxnewWishList($var) {
        parse_str($var, $this->post);
        $this->request = $this->post = array_stripslashes($this->post);
        $this->request = array_merge($this->request, $this->post);
        $result = $this->newWishList();

        $this->ajax->quickresult($result['code']['wishlist']);
    }

    //列表
    function wishlistList($data) {
        $data['sort']       = $data['sort'] ? $data['sort'] : 'a.updated';
        $data['sortorder']  = $data['sortorder'] ? $data['sortorder'] : 'DESC';
        $data['pagesize']   = $data['pagesize'] ? (intval($data['pagesize'])>50 ? 50 : intval($data['pagesize'])) : 20;

        $result = $this->model->wishlistList($data);
        $result['code']['wishlist'] = $result['code']['wishlistList'];

        $result['data'] = $data;

        return $result;
    }

    //测试物品是否在用户的wishlist中
    function isUserWish($iid, $uid=0) {
        if (!$uid) $uid = $this->session['id'];
        return $this->model->isUserWish($iid, $uid);
    }

    //Wishlist点击请求
    function wishRequest() {
        $data['md5']    = trim($this->request['wish']);
        $data['iid']    = intval($this->request['id']);
        return $this->model->wishRequest($data);
    }
}
?>